#include "process.h"
#include <string.h>
#include <unistd.h>

#define SIZE 101
#define STYLE '='

//v2:根据进度，动态刷新一次进度条
void FlushProcess(const char *tips, double total, double current)
{
  const char * lable = "|/-\\";
  int len = strlen(lable);
  static int index = 0;
  char buffer[SIZE];
  memset(buffer, 0, sizeof(buffer));

  //输出比率
  double rate = current / total * 100;
  //打印出多少个#
  int num = (int)rate;

  int i = 0;
  for(; i < num; i++)
    buffer[i] = STYLE;

  printf("%s...[%-100s][%.1f%%][%c]\r", tips, buffer, rate,lable[index++]);
  fflush(stdout);
  index %= len;

  if(num == 100) printf("\n");
}

//v1:展示进度条基本功能
void process()
{
  int rate = 0;
  char buffer[SIZE];
  //初始情况下buffer里面全是0即\0
  memset(buffer, 0, sizeof(buffer));
  const char* lable = "|/-\\";
  int len = strlen(lable);

  while(rate <= 100)
  {
    //预留出100个字符
    printf("[%-100s][%d%%][%c]\r", buffer, rate, lable[rate%len]);
    fflush(stdout);
    //每次多一个#
    buffer[rate] = STYLE;
    
    rate++;
    usleep(50000);
  }
  printf("\n");
}
